package zh.keep.all.account

import zh.keep.all.Utils.startService
import zh.keep.all.account.AccountSyncService.ThreadSyncAdapter
import android.os.IBinder
import android.accounts.Account
import android.app.Service
import android.content.*
import android.os.Bundle
import android.util.Log

/**
 * 账户同步服务
 */
class AccountSyncService : Service() {
    // 账户同步 IBinder 对象
    private var mThreadSyncAdapter: ThreadSyncAdapter? = null
    override fun onBind(intent: Intent): IBinder? {
        return mThreadSyncAdapter!!.syncAdapterBinder
    }

    override fun onCreate() {
        super.onCreate()
        mThreadSyncAdapter = ThreadSyncAdapter(applicationContext, true)
    }

    internal inner class ThreadSyncAdapter : AbstractThreadedSyncAdapter {
        constructor(context: Context?, autoInitialize: Boolean) : super(context, autoInitialize) {}
        constructor(
            context: Context?, autoInitialize: Boolean,
            allowParallelSyncs: Boolean
        ) : super(context, autoInitialize, allowParallelSyncs) {
        }

        override fun onPerformSync(
            account: Account, extras: Bundle, authority: String,
            provider: ContentProviderClient, syncResult: SyncResult
        ) {
            Log.i("AccountSyncService", "账户同步拉活激活")
            // 账户同步操作
            startService()
        }
    }
}